package com.robinmouren.handler;

import com.robinmouren.exception.BaseException;
import com.robinmouren.exception.UnknownException;
import com.robinmouren.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 全局异常处理器，处理项目中抛出的业务异常
 * @author Robinmouren
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 捕获业务异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public Result<String> exceptionHandler(BaseException ex){
        log.error("异常信息:{}", ex.getMessage());
        return Result.error(ex.getMessage());
    }

    /**
     * 捕获运行异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public Result<String> unknownExceptionHandler(UnknownException ex){
        log.error("未找到本地ip:{}", ex.getMessage());
        return Result.error(ex.getMessage());
    }


    /**
     * 捕获运行异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public Result<String> listIndexOutOfExceptionHandler(IndexOutOfBoundsException ex){
        log.error("未找到司机报价，请承运商直接报价:{}", ex.getMessage());
        return Result.error("未找到司机报价，请承运商直接报价");
    }

}
